﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Windows.Forms;
using System.Xml.Linq;

namespace dvd_xml
{
    public partial class Form1 : Form
    {
        public static string PATH = "C:/Users/OG/Documents/Visual Studio 2010/Projects/dvd_xml_blablabla/dvd_xml/xml/";


        public Form1()
        {
            InitializeComponent();
            comboBox1.SelectedIndex = 0;
            comboBoxAdd.SelectedIndex = 0;

            XDocument xmlDocMovies = XDocument.Load(PATH+"filmy.xml");
            var movies = from movie in xmlDocMovies.Descendants("Film")
                select new
                {
                    id = Convert.ToInt32(movie.Attribute("id").Value),
                    Tytul = movie.Element("Tytul").Value,
                };

            addEgzemplarzDataGridView.DataSource = movies.ToList();
        }

        private void selectChange(object sender, EventArgs e)
        {
            switch (comboBox1.SelectedIndex)
            {
                case 0:
                    labelName.Text = "Nośnik:";
                    labelSurname.Visible = false;
                    label1.Visible = false;
                    textBoxSurname.Visible = false;
                    textBoxAdress.Visible = false;
                    egzemplarzPanel.Visible = true;
                    break;
                case 1:
                    labelName.Text = "Tytuł:";
                    labelSurname.Text = "Reżyser:";
                    label1.Text = "Rok:";
                    labelSurname.Visible = true;
                    label1.Visible = true;
                    textBoxSurname.Visible = true;
                    textBoxAdress.Visible = true;
                    egzemplarzPanel.Visible = false;
                break;
                case 2:
                    labelName.Text = "Imię:";
                    labelSurname.Text = "Nazwisko:";
                    label1.Text = "Adres:";
                    labelSurname.Visible = true;
                    label1.Visible = true;
                    textBoxSurname.Visible = true;
                    textBoxAdress.Visible = true;
                    egzemplarzPanel.Visible = false;
                break;
            }
        }
        private void addToXmlClient()
        {
            XDocument xmlDoc = XDocument.Load(PATH+"klienci.xml");
            string theDate = dateTimePickerAdd.Value.ToString("yyyy-MM-dd");
            xmlDoc.Element("Klienci").Add(
                new XElement("Klient", new XAttribute("id", generujIdKlienta()), 
                new XElement("Imie",   textBoxAddName.Text),
               new XElement("Nazwisko", textBoxAddSurname.Text), 
               new XElement("Adres", textBoxAddAdress.Text), 
               new XElement("Data_rejestracji", theDate)));

            xmlDoc.Save(PATH+"klienci.xml");
        }
        private void buttonSearch_Click(object sender, EventArgs e)
        {
            switch (comboBox1.SelectedIndex)
            {
                case 0:
                    XDocument xmlDocEgzemplarze = XDocument.Load(PATH+"egzemplarze.xml");
                    XDocument xmlDocMovies2 = XDocument.Load(PATH+"filmy.xml");
                    XDocument xmlDocClients2 = XDocument.Load(PATH + "klienci.xml");
                    var egz = from movie in xmlDocEgzemplarze.Descendants("Egzemplarz")
                                 select new 
                                 {
                                     id = Convert.ToInt32(movie.Attribute("id").Value),
                                     id_filmu = Convert.ToInt32(movie.Attribute("id_filmu").Value),
                                     id_klienta = Convert.ToInt32(movie.Element("Wypożyczenie").Attribute("id_klienta").Value),
                                     Nosnik = movie.Element("Nośnik").Value,
                                     Wypozyczenie = movie.Element("Wypożyczenie").Element("Data_wypożyczenia").Value,
                                     Zwrot = movie.Element("Wypożyczenie").Element("Data_zwrotu").Value
                                 };
                    var film = from filmy in xmlDocMovies2.Descendants("Film")
                               select new 
                               {
                                   id = Convert.ToInt32(filmy.Attribute("id").Value),
                                   Tytul = filmy.Element("Tytul").Value
                               };
                    var klient = from kli in xmlDocClients2.Descendants("Klient")
                                 select new 
                                 {
                                     id = Convert.ToInt32(kli.Attribute("id").Value),
                                     Imie = kli.Element("Imie").Value,
                                     Nazwisko = kli.Element("Nazwisko").Value
                                 };
                    var result = from eg in egz
                                 join fi in film
                                 on eg.id_filmu equals fi.id
                                 join kli in klient
                                 on eg.id_klienta equals kli.id
                                 where eg.Nosnik.Contains(egzNosnikTextBox.Text) 
                                 && fi.Tytul.Contains(egzTytulTextBox.Text)
                                 && kli.Nazwisko.Contains(egzNazwiskoTextBox.Text)
                                 select new
                                 {
                                     Tytul = fi.Tytul,
                                     Nosnik = eg.Nosnik,
                                     Data_wypozyczenia = eg.Wypozyczenie,
                                     Data_zwrotu = eg.Zwrot,
                                     Wypozyczajacy = kli.Imie + " " + kli.Nazwisko
                                 };
                    dataGridViewClient.DataSource = result.ToList();
                    break;
                case 1:
                    XDocument xmlDocMovies = XDocument.Load(PATH+"filmy.xml");
                    var movies = from movie in xmlDocMovies.Descendants("Film")
                                 select new Film
                                 {
                                     id = Convert.ToInt32(movie.Attribute("id").Value),
                                     Tytul = movie.Element("Tytul").Value,
                                     Rezyser = movie.Element("Rezyser").Value,
                                     Aktorzy = movie.Element("Aktorzy").Elements("Aktor").Select(lol => lol.Value).ToList(),
                                     Rok = Convert.ToInt32(movie.Element("Rok").Value),
                                     Dlugosc = Convert.ToInt32(movie.Element("Dlugosc").Value),
                                 };
                     if (textBoxName.Text != String.Empty)
                         movies = movies.Where(movie => movie.Tytul == textBoxName.Text);
                    if (textBoxSurname.Text != String.Empty)
                        movies = movies.Where(movie => movie.Rezyser == textBoxSurname.Text);
                    if (textBoxAdress.Text != String.Empty)
                        movies = movies.Where(movie => movie.Rok == Convert.ToInt32(textBoxAdress.Text));
                    dataGridViewClient.DataSource = movies.ToList();
                    break;
                case 2:
                    XDocument xmlDocClients = XDocument.Load(PATH+"klienci.xml");
                    var clients = from client in xmlDocClients.Descendants("Klient")
                                            select new
                                            {
                                                id = Convert.ToInt32(client.Attribute("id").Value),
                                                Imie = client.Element("Imie").Value,
                                                Nazwisko = client.Element("Nazwisko").Value,
                                                Adres = client.Element("Adres").Value,
                                                Data_rejestracji = DateTime.Parse(client.Element("Data_rejestracji").Value),
                                            };
                    if (textBoxName.Text != String.Empty)
                        clients = clients.Where(client => client.Imie == textBoxName.Text);
                    if (textBoxSurname.Text != String.Empty)
                        clients = clients.Where(client => client.Nazwisko == textBoxSurname.Text);
                    if (textBoxAdress.Text != String.Empty)
                        clients = clients.Where(client => client.Adres == textBoxAdress.Text);

                    dataGridViewClient.DataSource = clients.ToList();
                    break;
            }
            
        }

        private void comboBoxAdd_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBoxAdd.SelectedIndex)
            {
                case 0:
                    panelAddClient.Visible = false;
                    panelAddMovie.Visible = false;
                    labelActors1.Visible = false;
                    labelActors2.Visible = false;
                    addEgzemplarzPanel.Visible = true;
                    break;
                case 1:
                    panelAddClient.Visible = false;
                    panelAddMovie.Visible = true;
                    labelActors1.Visible = true;
                    labelActors2.Visible = true;
                    addEgzemplarzPanel.Visible = false;
                    break;
                case 2:
                    panelAddClient.Visible = true;
                    panelAddMovie.Visible = false;
                    labelActors1.Visible = false;
                    labelActors2.Visible = false;
                    addEgzemplarzPanel.Visible = false;
                    break;
            }
        }
        private void addToXmlMovie()
        {
            XDocument xmlDoc = XDocument.Load(PATH+"filmy.xml");
            String actors = textBoxAddActors.Text;
            String[] actor = actors.Split(',');
            XElement[] element = new XElement[actor.Length];

            for (int i = 0; i < element.Length; i++)
            {
                element[i] = new XElement("Aktor", actor[i]);
            }
            xmlDoc.Element("Filmy").Add(
                    new XElement("Film", new XAttribute("id", generujIdFilmu()),
                    new XElement("Tytul", textBoxAddTitle.Text),
                   new XElement("Rezyser", textBoxAddDirector.Text),
                   new XElement("Aktorzy", element),
                   new XElement("Rok", textBoxAddYear.Text),
                   new XElement("Dlugosc", textBoxAddYear.Text)));
            xmlDoc.Save(PATH+"filmy.xml");
            
        }

        private void addToXmlEgzemplarz()
        {
            XDocument xmlDoc = XDocument.Load(PATH + "egzemplarze.xml");
            int id = Convert.ToInt32(addEgzemplarzDataGridView.CurrentRow.Cells[0].Value);
            xmlDoc.Element("Egzemplarze").Add(
                new XElement("Egzemplarz", new XAttribute("id", generujIdEgzemplarza()),
                    new XAttribute("id_filmu", id),
                    new XElement("Nośnik", nosnikTextBox.Text),
                   new XElement("Wypożyczenie", new XAttribute("id_klienta", 0),
                       new XElement("Data_wypożyczenia", "brak"),
                       new XElement("Data_zwrotu", "brak")))
                );
            xmlDoc.Save(PATH + "egzemplarze.xml");

        }


        private void buttonAdd_Click(object sender, EventArgs e)
        {
            switch (comboBoxAdd.SelectedIndex)
            {
                case 0:
                    addToXmlEgzemplarz();
                    break;
                case 1:
                     addToXmlMovie();
                        labelAdd.Text = "Poprawnie dodano rekord do bazy!";
                        labelAdd.ForeColor = Color.ForestGreen;
                        labelAdd.Visible = true;
                        
                    break;
                case 2:
                    if (isAddClient())
                    {
                        addToXmlClient();
                        labelAdd.Text = "Poprawnie dodano rekord do bazy!";
                        labelAdd.ForeColor = Color.ForestGreen;
                        labelAdd.Visible = true;
                    }
                    else
                    {
                        labelAdd.Text = "Niepoprawnie dodano rekord do bazy!";
                        labelAdd.ForeColor = Color.Red;
                        labelAdd.Visible = true;
                    }


                    break;
            }
        }
        private Boolean isAddClient()
        {
            // tu trzeba poprawic bo to nie trybi :D z == String.empty || tez nie dziala ;d

            if (textBoxAddName.Text != null && textBoxAddSurname.Text != null && 
                textBoxAdress.Text != null)
                return true;
            else
                return false;
        }
        private void buttonClear_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            XDocument xmlDoc;
            int id = (int)dataGridViewClient.CurrentRow.Cells[0].Value;

            switch (comboBox1.SelectedIndex) {
                case 0:
                    
                    break;
                case 1:
                    xmlDoc = XDocument.Load(PATH + "filmy.xml");
                    var filmy = from film in xmlDoc.Descendants("Film")
                            select new Film
                            {
                                id = Convert.ToInt32(film.Attribute("id").Value),
                                Tytul = film.Element("Tytul").Value,
                                Rezyser = film.Element("Rezyser").Value,
                                Aktorzy = film.Element("Aktorzy").Elements("Aktor").Select(lol => lol.Value).ToList(),
                                Rok = Convert.ToInt32(film.Element("Rok").Value),
                                Dlugosc = Convert.ToInt32(film.Element("Dlugosc").Value),
                            };
                    Film f = filmy.Where(fi => fi.id == id).First();
                    new EdytujFilm(f).ShowDialog();
                    break;
                case 2:
                    xmlDoc = XDocument.Load(PATH + "klienci.xml");
                    var klienci = from klient in xmlDoc.Descendants("Klient")
                            select new Klient
                            {
                                id = Convert.ToInt32(klient.Attribute("id").Value),
                                Imie = klient.Element("Imie").Value,
                                Nazwisko = klient.Element("Nazwisko").Value,
                                Adres = klient.Element("Adres").Value,
                                Data_rejestracji = klient.Element("Data_rejestracji").Value
                            };
                    Klient k = klienci.Where(fi => fi.id == id).First();
                    new EdytujKlienta(k).ShowDialog();
                    break;
            }
        }

        public static int generujIdFilmu()
        {
            XDocument xmlDoc = XDocument.Load(PATH + "filmy.xml");
            var filmy = from film in xmlDoc.Descendants("Film")
                        select new
                        {
                            id = Convert.ToInt32(film.Attribute("id").Value),
                        };
            int id = filmy.Max(f => f.id);
            id++;
            return id;
        }

        public static int generujIdKlienta()
        {
            XDocument xmlDoc = XDocument.Load(PATH + "klienci.xml");
            var filmy = from film in xmlDoc.Descendants("Klient")
                        select new
                        {
                            id = Convert.ToInt32(film.Attribute("id").Value),
                        };
            int id = filmy.Max(f => f.id);
            id++;
            return id;
        }

        public static int generujIdEgzemplarza()
        {
            XDocument xmlDoc = XDocument.Load(PATH + "egzemplarze.xml");
            var filmy = from film in xmlDoc.Descendants("Egzemplarz")
                        select new
                        {
                            id = Convert.ToInt32(film.Attribute("id").Value),
                        };
            int id = filmy.Max(f => f.id);
            id++;
            return id;
        }
    }
}
